//
//  ILikedViewController.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/4.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "ILikedViewController.h"
#import "ILikedViewModel.h"
#import "SPPageMenu.h"
#define pageMenuH kHeightProportion_In_iPhoneTriploid(44)
#define NaviH (iPhoneX ? 88 : 64) // 812是iPhoneX的高度
#define scrollViewHeight (KScreenHeight-NaviH-pageMenuH)

@interface ILikedViewController ()<SPPageMenuDelegate, UIScrollViewDelegate>
{
    NSArray * p_Arr;
}
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, weak) SPPageMenu *pageMenu;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *myChildViewControllers;
@property (nonatomic, assign) BOOL butSelect;
@property (nonatomic, assign) NSInteger nowIndex;
@property (nonatomic, strong) UIButton *rightBut;

@end

@implementation ILikedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我喜欢的";
    [self addNavigationItemWithTitles:@[@"编辑"] isLeft:NO target:self action:@selector(manageAction:) tags:@[@43] theButTitColor:[UIColor colorWithHexString:@"#666666"] titFont:SYSTEMFONT(15)];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage imageWithColor:[UIColor colorWithHexString:@"#F2F2F2"]]init]
                                                  forBarMetrics:UIBarMetricsDefault];
    //    self.navigationItem.titleView  = [self createGoodsNavView];
    self.dataArr = @[@"文章", @"帖子", @"回复"];
    // trackerStyle:跟踪器的样式
    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
    pageMenu.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    pageMenu.selectedItemTitleColor = [UIColor colorWithHexString:@"#A89A60"];
    pageMenu.unSelectedItemTitleColor = [UIColor colorWithHexString:@"#333333"];
    pageMenu.itemTitleFont = SYSTEMFONT(14);
    pageMenu.trackerWidth = kWidthProportion_In_iPhoneTriploid(29);
    pageMenu.tracker.backgroundColor = [UIColor colorWithHexString:@"#A89A60"];
    pageMenu.contentInset = UIEdgeInsetsMake(0, (KScreenWidth-kWidthProportion_In_iPhoneTriploid(260))/2, 0, (KScreenWidth-kWidthProportion_In_iPhoneTriploid(260))/2);//{top, left, bottom, right};
    pageMenu.itemPadding = kWidthProportion_In_iPhoneTriploid(66);
    pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollAdaptContent;
    pageMenu.dividingLine.hidden = YES;
    // 传递数组，默认选中第2个
    [pageMenu setItems:self.dataArr selectedItemIndex:0];
    // 设置代理
    pageMenu.delegate = self;
    // 给pageMenu传递外界的大scrollView，内部监听self.scrollView的滚动，从而实现让跟踪器跟随self.scrollView移动的效果
    pageMenu.bridgeScrollView = self.scrollView;
    [self.view addSubview:pageMenu];
    _pageMenu = pageMenu;
    [self.view addSubview:self.scrollView];
    NSArray *controllerClassNames = [NSArray arrayWithObjects:@"ILikedListViewController",@"PostsListViewController",@"ReturnCardViewController", nil];
    for (int i = 0; i < self.dataArr.count; i++) {
        if (controllerClassNames.count > i) {
            GLGWBaseViewController *baseVc = [[NSClassFromString(controllerClassNames[i]) alloc] init];
            [self addChildViewController:baseVc];
            // 控制器本来自带childViewControllers,但是遗憾的是该数组的元素顺序永远无法改变，只要是addChildViewController,都是添加到最后一个，而控制器不像数组那样，可以插入或删除任意位置，所以这里自己定义可变数组，以便插入(删除)(如果没有插入(删除)功能，直接用自带的childViewControllers即可)
            [self.myChildViewControllers addObject:baseVc];
        }
    }
    
    // pageMenu.selectedItemIndex就是选中的item下标
    if (self.pageMenu.selectedItemIndex < self.myChildViewControllers.count) {
        GLGWBaseViewController *baseVc = self.myChildViewControllers[self.pageMenu.selectedItemIndex];
        [self.scrollView addSubview:baseVc.view];
        baseVc.view.frame = CGRectMake(KScreenWidth*self.pageMenu.selectedItemIndex, 0, KScreenWidth, scrollViewHeight);
        self.scrollView .contentOffset = CGPointMake(KScreenWidth*self.pageMenu.selectedItemIndex, 0);
        self.scrollView .contentSize = CGSizeMake(self.dataArr.count*KScreenWidth, 0);
    }
}

- (void)manageAction:(UIButton *)sender{
    self.rightBut = sender;
    if (self.butSelect) {
        [sender setTitle:@"完成" forState:UIControlStateNormal];
        self.butSelect = NO;
    }else{
        [sender setTitle:@"管理" forState:UIControlStateNormal];
        self.butSelect = YES;
    }
    //注册登录状态监听
    p_Arr = @[@(self.nowIndex),@(self.butSelect)];
    KPostNotification(@"EditAction", p_Arr);
}

-(void)cancelAction
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - SPPageMenu的代理方法

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index
{
    NSLog(@"%zd",index);
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex
{
    [self.rightBut setTitle:@"管理" forState:UIControlStateNormal];
    NSLog(@"%zd------->%zd",fromIndex,toIndex);
    self.butSelect = YES;
    self.nowIndex = toIndex;
    p_Arr = @[@(self.nowIndex),@(self.butSelect)];
    KPostNotification(@"EditAction", p_Arr);
    
    // 如果该代理方法是由拖拽self.scrollView而触发，说明self.scrollView已经在用户手指的拖拽下而发生偏移，此时不需要再用代码去设置偏移量，否则在跟踪模式为SPPageMenuTrackerFollowingModeHalf的情况下，滑到屏幕一半时会有闪跳现象。闪跳是因为外界设置的scrollView偏移和用户拖拽产生冲突
    if (!self.scrollView.isDragging) { // 判断用户是否在拖拽scrollView
        // 如果fromIndex与toIndex之差大于等于2,说明跨界面移动了,此时不动画.
        if (labs(toIndex - fromIndex) >= 2) {
            [self.scrollView setContentOffset:CGPointMake(KScreenWidth * toIndex, 0) animated:NO];
        } else {
            [self.scrollView setContentOffset:CGPointMake(KScreenWidth * toIndex, 0) animated:YES];
        }
    }
    
    if (self.myChildViewControllers.count <= toIndex) {return;}
    
    UIViewController *targetViewController = self.myChildViewControllers[toIndex];
    // 如果已经加载过，就不再加载
    if ([targetViewController isViewLoaded]) return;
    
    targetViewController.view.frame = CGRectMake(KScreenWidth * toIndex, 0, KScreenWidth, scrollViewHeight);
    [_scrollView addSubview:targetViewController.view];
}

#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    // 这一步是实现跟踪器时刻跟随scrollView滑动的效果,如果对self.pageMenu.scrollView赋了值，这一步可省
    // [self.pageMenu moveTrackerFollowScrollView:scrollView];
}


#pragma mark - getter

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, pageMenuH, KScreenWidth, scrollViewHeight)];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
    }
    return  _scrollView;
}

- (NSMutableArray *)myChildViewControllers {
    
    if (!_myChildViewControllers) {
        _myChildViewControllers = [NSMutableArray array];
        
    }
    return _myChildViewControllers;
}

- (void)dealloc {
    NSLog(@"父控制器被销毁了");
}



@end
